// SkinPanel.cpp : implementation file
//

#include "stdafx.h"
#include "LoPlayer.h"
#include "SkinPanel.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define DEFAULT_BORDER_WIDTH 10
// CSkinPanel

IMPLEMENT_DYNAMIC(CSkinPanel, CStatic)

CSkinPanel::CSkinPanel()
		: m_border(0)
{

}

//=============================================================================
CSkinPanel::~CSkinPanel()
{
}

//=============================================================================
BOOL CSkinPanel::CreateInternal( const RECT& rect, CWnd* pParentWnd, UINT nID )
{
	CRect rt(rect);
	ASSERT(m_pbmPanel);
	rt.right = rt.left + m_pbmPanel->GetWidth();
	rt.bottom = rt.top + m_pbmPanel->GetHeight();

	if (!CStatic::Create(m_strCommandName, SS_OWNERDRAW | WS_VISIBLE, rt, pParentWnd, nID)) {
		return FALSE;
	}

	return TRUE;
}

//=============================================================================
BOOL CSkinPanel::LoadSkinImage( LPCTSTR lpszFilename )
{
	m_pbmPanel.Attach(Gdiplus::Bitmap::FromFile (lpszFilename));
	if (!m_pbmPanel)
		return FALSE;

	SetBorder(DEFAULT_BORDER_WIDTH);

	return m_pbmLeft.m_p != NULL;
}

//=============================================================================
void CSkinPanel::DrawIt( Gdiplus::Bitmap* pbmWnd, const RECT& rect )
{
	ASSERT(pbmWnd);
	ASSERT(m_pbmPanel);

	const int cxRect = (rect.right - rect.left);
	const int cyRect = rect.bottom - rect.top;
	SetWindowPos(NULL,
	             rect.left - m_cxBorder,
	             rect.top - m_cyBorder,
	             cxRect,	cyRect,
	             SWP_NOREDRAW);

	//  [6/15/2008 Dean]
	if (m_pbmPanel->GetWidth() != cxRect ||	m_pbmPanel->GetHeight() != cyRect) {
		m_pbmPanel.Free();
		m_pbmPanel.Attach(GetResizePanelBitmap(cxRect, cyRect));
	}

	Gdiplus::Graphics graphic(pbmWnd);
	graphic.DrawImage(m_pbmPanel, rect.left, rect.top, m_pbmPanel->GetWidth(), m_pbmPanel->GetHeight());
	// end test

}

//=============================================================================
CRect CSkinPanel::GetPlacement( const INT cxFrame, const INT cyFrame )
{
	CRect rt = CSkinControl::GetPlacement(cxFrame, cyFrame);
	switch (m_Align) {
	case TopLeft:
	case TopRight:
	case BottomLeft:
	case BottomRight:
		rt.right = rt.left + m_pbmPanel->GetWidth();	// don't change size
		rt.bottom = rt.top + m_pbmPanel->GetHeight();
		break;
	case TopCenter:
	case BottomCenter:
		rt.right = rt.left + (cxFrame - 2 * m_Offset.x);	// stretch horizon
		rt.bottom = rt.top + m_pbmPanel->GetHeight();
		break;
	default:
		ASSERT(FALSE);
	}

	return rt;
}

BEGIN_MESSAGE_MAP(CSkinPanel, CStatic)
END_MESSAGE_MAP()



// CSkinPanel message handlers



void CSkinPanel::DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/)
{

	// TODO:  Add your code to draw the specified item
	//UpdateSkin();
}

void CSkinPanel::SetBorder( const int border )
{
	ASSERT(m_pbmPanel);
	if (border == m_border)
		return;

	m_border = border;
	m_pbmLeft.Free();
	m_pbmCenter.Free();
	m_pbmRight.Free();

	const int cx = m_pbmPanel->GetWidth();
	const int cy = m_pbmPanel->GetHeight();

	m_pbmLeft.Attach(m_pbmPanel->Clone (0, 0, m_border, cy, PixelFormatDontCare));
	m_pbmCenter.Attach(m_pbmPanel->Clone (m_border, 0, 1, cy, PixelFormatDontCare));
	m_pbmRight.Attach(m_pbmPanel->Clone (cx - m_border, 0, m_border, cy, PixelFormatDontCare));

}

Gdiplus::Bitmap* CSkinPanel::GetResizePanelBitmap( const int cx, const int cy )
{
	Gdiplus::Bitmap bmpPanel(cx, cy, PixelFormat32bppARGB);

	Gdiplus::Graphics graPanel(&bmpPanel);

	graPanel.DrawImage(m_pbmLeft, 0, 0, m_pbmLeft->GetWidth(), m_pbmLeft->GetHeight());

	Gdiplus::CachedBitmap  chbmTop(m_pbmCenter, &graPanel);
	const int cxCenter = cx - 2 * m_border;
	for (INT xPos = 0; xPos < cxCenter; xPos++) {
		graPanel.DrawCachedBitmap (&chbmTop, xPos + m_border, 0);
	}

	graPanel.DrawImage(m_pbmRight, cxCenter + m_border, 0, m_pbmRight->GetWidth(), m_pbmRight->GetHeight());

	return bmpPanel.Clone(0, 0, cx, cy, PixelFormatDontCare);
}